package com.pactera.madp.cp.service.sync.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pactera.madp.cp.api.dto.sync.MaMarketingLeadDTO;
import com.pactera.madp.cp.api.dto.sync.SyncDTO;
import com.pactera.madp.cp.api.entity.sync.MaYesterdayDataBoardEntity;
import com.pactera.madp.cp.common.enums.sync.LeadEnum;
import com.pactera.madp.cp.common.enums.sync.MaEventEnum;
import com.pactera.madp.cp.service.sync.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @title 神策-日终批量-业务处理实现
 *  - ① 处理同步参数(数据日期)
 *  - ② 汇总数据日期数据到数据看板历史表
 *  - ③ 清空数据看板数据
 *  - ④ 汇总数据看板数据
 *  - ⑤ 汇总产品偏好数据
 *  - ⑥ 删除数据日期一周前营销线索数据
 *  - ⑦ 汇总昨日营销线索数据
 *  - ⑧ 删除数据日期一周前雷达数据
 * @date 2022-05-13
 * @author Deming.Chang
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SyncSaEndDayServiceImpl implements SyncSaEndDayService {

    /**
     * 数据采集事件实现[对接入库]
     */
    private final SyncEventService eventService;

    /**
     * 当日看板数据统计实现
     */
    private final MaTodayDataBoardService todayDataBoardService;

    /**
     * 昨日看板数据统计实现
     */
    private final MaYesterdayDataBoardService yesterdayDataBoardService;

    /**
     * 历史看板数据统计实现
     */
    private final MaHisDataBoardService hisDataBoardService;

    /**
     * 营销线索处理实现
     */
    private final MaMarketingLeadService marketingLeadService;

    /**
     * 销售线索明细处理实现
     */
    private final MaMarketingLeadDetailsService maMarketingLeadDetailsService;

    /**
     * 客户雷达处理实现
     */
    private final MaRadarService radarService;

    /**
     * ① 处理同步参数(数据日期)
     */
    @Override
    public String handleSyncParams(SyncDTO dto) {
        log.info("--->Ma 1、处理同步参数(数据日期)<---");
        Map<String, Object> params = dto.getParams();
        if (CollUtil.isEmpty(params) || ObjectUtil.isNull(MapUtil.getStr(params, PARAM_KEY_MA_DATE))) {
            return DateUtil.format(DateUtil.yesterday(), DatePattern.NORM_DATE_PATTERN);
        }
        return MapUtil.getStr(params, PARAM_KEY_MA_DATE);
    }

    /**
     * ② 汇总数据日期数据到数据看板历史表
     * - 删除数据日期历史数据
     * - 将数据采集(cp_sync_event)数据日期数据汇总到历史表(cp_ma_his_data_board)[数据日期：访客浏览当天数据]
     */
    @Override
    public void summaryMaDateHisDataBoard(String maDate) {
        log.info("--->Ma 2、开始汇总昨日数据到数据看板历史表<---");
        hisDataBoardService.insertMaDateData(maDate);
        log.info("--->Ma 2、结束汇总昨日数据到数据看板历史表<---");
    }

    /**
     * ③ 清空数据看板数据
     * - 清空当日数据看板数据
     * - 清空昨日数据看板数据
     */
    @Override
    public void cleanDataBoard() {
        log.info("--->Ma 3、开始清空数据看板数据<---");
        // 1.0 清空当日数据看板数据
        todayDataBoardService.remove(Wrappers.emptyWrapper());
        // 2.0 清空昨日数据看板数据
        yesterdayDataBoardService.remove(Wrappers.emptyWrapper());
        log.info("--->Ma 3、结束清空数据看板数据<---");
    }

    /**
     * ④ 汇总数据看板数据
     * - 汇总数据日期近三日数据
     * - 汇总数据日期近七日数据
     */
    @Override
    public void summaryDataBoard(String maDate) {
        log.info("--->Ma 4、开始汇总数据看板数据<---");
        // 1.0 汇总昨日近三日数据
        String endDate = DateUtil.format(DateUtil.offsetDay(DateUtil.parse(maDate), -3), DatePattern.NORM_DATE_PATTERN);
        List<MaYesterdayDataBoardEntity> entities = eventService.countThreeDate(maDate, endDate);
        log.info("--->Ma -- 近三日数据:{}条 -- <---", entities.size());
        if(CollUtil.isNotEmpty(entities)){
            yesterdayDataBoardService.saveOrUpdateBatch(entities);
        }
        // 2.0 汇总昨日近七日数据
        endDate = DateUtil.format(DateUtil.offsetDay(DateUtil.parse(maDate), -7), DatePattern.NORM_DATE_PATTERN);
        entities = eventService.countSevenDate(maDate, endDate);
        log.info("--->Ma -- 近七日数据:{}条 -- <---", entities.size());
        if(CollUtil.isNotEmpty(entities)){
            yesterdayDataBoardService.saveOrUpdateBatch(entities);
        }
        log.info("--->Ma 4、开始汇总数据看板数据<---");
    }

    /**
     * ⑤ 汇总产品偏好数据
     */
    @Override
    public void summaryPrdPreference(String maDate) {
        log.info("--->Ma 5、开始汇总产品偏好数据<---");
        // 1.0 汇总昨日近三日数据
        String endDate = DateUtil.format(DateUtil.offsetDay(DateUtil.parse(maDate), -3), DatePattern.NORM_DATE_PATTERN);
        // 1.1 理财
        List<MaYesterdayDataBoardEntity> entities = eventService.countThreeDateFinancial(maDate, endDate);
        log.info("--->Ma -- 近三日理财数据:{}条 -- <---", entities.size());
        if(CollUtil.isNotEmpty(entities)){
            yesterdayDataBoardService.saveOrUpdateBatch(entities);
        }
        // 1.2 基金
        entities = eventService.countThreeDateFund(maDate, endDate);
        log.info("--->Ma -- 近三日基金数据:{}条 -- <---", entities.size());
        if(CollUtil.isNotEmpty(entities)){
            yesterdayDataBoardService.saveOrUpdateBatch(entities);
        }
        // 1.3 存款
        entities = eventService.countThreeDateDeposit(maDate, endDate);
        log.info("--->Ma -- 近三日存款数据:{}条 -- <---", entities.size());
        if(CollUtil.isNotEmpty(entities)){
            yesterdayDataBoardService.saveOrUpdateBatch(entities);
        }
        // 1.4 贷款
        entities = eventService.countThreeDateLoan(maDate, endDate);
        log.info("--->Ma -- 近三日贷款数据:{}条 -- <---", entities.size());
        if(CollUtil.isNotEmpty(entities)){
            yesterdayDataBoardService.saveOrUpdateBatch(entities);
        }
        // 1.5 信用卡
        entities = eventService.countThreeDateCredit(maDate, endDate);
        log.info("--->Ma -- 近三日信用卡数据:{}条 -- <---", entities.size());
        if(CollUtil.isNotEmpty(entities)){
            yesterdayDataBoardService.saveOrUpdateBatch(entities);
        }

        // 2.0 汇总昨日近七日数据
        endDate = DateUtil.format(DateUtil.offsetDay(DateUtil.parse(maDate), -7), DatePattern.NORM_DATE_PATTERN);
        // 2.1 理财
        entities = eventService.countSevenDateFinancial(maDate, endDate);
        log.info("--->Ma -- 近七日理财数据:{}条 -- <---", entities.size());
        if(CollUtil.isNotEmpty(entities)){
            yesterdayDataBoardService.saveOrUpdateBatch(entities);
        }
        // 2.2 基金
        entities = eventService.countSevenDateFund(maDate, endDate);
        log.info("--->Ma -- 近七日基金数据:{}条 -- <---", entities.size());
        if(CollUtil.isNotEmpty(entities)){
            yesterdayDataBoardService.saveOrUpdateBatch(entities);
        }
        // 2.3 存款
        entities = eventService.countSevenDateDeposit(maDate, endDate);
        log.info("--->Ma -- 近七日存款数据:{}条 -- <---", entities.size());
        if(CollUtil.isNotEmpty(entities)){
            yesterdayDataBoardService.saveOrUpdateBatch(entities);
        }
        // 2.4 贷款
        entities = eventService.countSevenDateLoan(maDate, endDate);
        log.info("--->Ma -- 近七日贷款数据:{}条 -- <---", entities.size());
        if(CollUtil.isNotEmpty(entities)){
            yesterdayDataBoardService.saveOrUpdateBatch(entities);
        }
        // 2.5 信用卡
        entities = eventService.countSevenDateCredit(maDate, endDate);
        log.info("--->Ma -- 近七日信用卡数据:{}条 -- <---", entities.size());
        if(CollUtil.isNotEmpty(entities)){
            yesterdayDataBoardService.saveOrUpdateBatch(entities);
        }
        log.info("--->Ma 5、结束汇总产品偏好数据<---");
    }

    /**
     * ⑥ 删除数据日期一周前营销线索数据
     * - 删除数据日期营销线索首页数据(7天前数据)
     * - 删除数据日期营销线索明细数据(7天前数据)
     * @param maDate 数据日期
     */
    @Override
    public void cleanLeads(String maDate) {
        log.info("--->Ma 6、开始清空历史营销线索数据<---");
        // 1.0 清空历史营销线索首页数据
        int count = marketingLeadService.removeHisData(maDate, -7);
        log.info("--->Ma -- 删除数据日期一周前销售线索首页数据:{}条 -- <---", count);
        // 2.0 清空历史营销线索明细数据
        count = maMarketingLeadDetailsService.removeHisData(maDate, -7);
        log.info("--->Ma -- 删除数据日期一周前销售线索明细数据:{}条 -- <---", count);
        log.info("--->Ma 6、结束清空历史营销线索数据<---");
    }

    /**
     * ⑦ 汇总昨日营销线索数据
     */
    @Override
    public void summaryLeads(String maDate) {
        log.info("--->Ma 7、开始汇总昨日营销线索数据<---");
        // 1.0 理财营销线索
        MaMarketingLeadDTO financialDto = new MaMarketingLeadDTO();
        financialDto.setLeadType(LeadEnum.FINANCIAL.getType())
                .setLeadName(LeadEnum.FINANCIAL.getName())
                .setMaDate(maDate)
                .setViewEvent(MaEventEnum.FINANCIAL_VIEW.getEvent())
                .setClickEvent(MaEventEnum.FINANCIAL_BYE_CLICK.getEvent())
                .setFinancialType(LeadEnum.FINANCIAL.getName());
        marketingLeadService.summaryAndSave(financialDto);

        // 2.0 基金营销线索
        MaMarketingLeadDTO fundDto = new MaMarketingLeadDTO();
        fundDto.setLeadType(LeadEnum.FUND.getType())
                .setLeadName(LeadEnum.FUND.getName())
                .setMaDate(maDate)
                .setViewEvent(MaEventEnum.FINANCIAL_VIEW.getEvent())
                .setClickEvent(MaEventEnum.FINANCIAL_BYE_CLICK.getEvent())
                .setFinancialType(LeadEnum.FUND.getName());
        marketingLeadService.summaryAndSave(fundDto);

        // 3.0 贷款营销线索
        MaMarketingLeadDTO loanDto = new MaMarketingLeadDTO();
        loanDto.setLeadType(LeadEnum.LOAN.getType())
                .setLeadName(LeadEnum.LOAN.getName())
                .setMaDate(maDate)
                .setViewEvent(MaEventEnum.LOAN_VIEW.getEvent())
                .setClickEvent(MaEventEnum.LOAN_APP_CLICK.getEvent());
        marketingLeadService.summaryAndSave(loanDto);

        // 4.0 贷款营销线索
        MaMarketingLeadDTO creditDto = new MaMarketingLeadDTO();
        creditDto.setLeadType(LeadEnum.CREDIT.getType())
                .setLeadName(LeadEnum.CREDIT.getName())
                .setMaDate(maDate)
                .setViewEvent(MaEventEnum.CREDIT_VIEW.getEvent())
                .setClickEvent(MaEventEnum.CREDIT_APP_CLICK.getEvent());
        marketingLeadService.summaryAndSave(creditDto);

        // 5.0 存款营销线索
        MaMarketingLeadDTO depositDto = new MaMarketingLeadDTO();
        depositDto.setLeadType(LeadEnum.DEPOSIT.getType())
                .setLeadName(LeadEnum.DEPOSIT.getName())
                .setMaDate(maDate)
                .setViewEvent(MaEventEnum.DEPOSIT_VIEW.getEvent())
                .setClickEvent(MaEventEnum.DEPOSIT_IN_CLICK.getEvent());
        marketingLeadService.summaryAndSave(depositDto);

        // 6.0 营销线索明细数据
        maMarketingLeadDetailsService.summaryAndSave(maDate);
        log.info("--->Ma 7、开始汇总昨日营销线索数据<---");
    }

    /**
     * ⑧ 删除数据日期一周前雷达数据
     * @param maDate 数据日期
     */
    @Override
    public void cleanRadar(String maDate) {
        log.info("--->Ma 8、开始删除数据日期一周前雷达数据<---");
        int count = radarService.removeHisData(maDate, -7);
        log.info("--->Ma -- 删除数据日期一周前雷达数据:{}条 -- <---", count);
        log.info("--->Ma 8、结束删除数据日期一周前雷达数据<---");
    }

}
